<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			site_specific/members.php
#	FUNCTION:		Handles the Members-only page that displays businesses and their staff
#	AUTHOR:			Cameron Morrow
#	CREATED:		28/11/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# GET PASSED VARS
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Passed business
$passed_business_id = (@$_GET["business"]) ? removeInvalidChars($_GET["business"], $VALID_CHAR_LIST["namesupper"]) : false;

# Business search
$business_search = (@$_GET["businesssearch"]) ? removeInvalidChars($_GET["businesssearch"], $VALID_CHAR_LIST["namesupper"]) : "";

# Was a business passed?
if ($passed_business_id !== false) {

	# SQL
	$sql = "SELECT u_id, u_login_name, u_url, u_category, u_region, u_email, u_phone, u_fax, u_mobile, u_address, u_desc, u_image FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_id = '" . $passed_business_id . "'";

	# Get data
	$data = getRecord($sql);

	if ($data !== false) {

		# Get first result
		$data = $data[0];

		# Set header
		$PAGE_NAME = $data["u_login_name"];

		# Image
		if ($data["u_image"] != "") {
			$image = explode("|", $data["u_image"]);
			$image = "<img src=\"../assets/uploads/" . $image[0] . "\" alt=\"Image\" style=\"float: right; margin: 6px; padding: 2px; border: 1px solid #999;\" width=\"100\" />";
		} else {
			$image = "";
		}

		# Get categories
		$categories_text = array();
		if ($data["u_category"] != "") {
			$data["u_category"] = explode(",", $data["u_category"]);
			foreach ($data["u_category"] as $category_id) {
				$categories_text[] = $EXTERNAL_CONSTANTS["sponsor_categories"][$category_id];
			}
			$categories_text = implode(", ", $categories_text);
		} else {
			$categories_text = "";
		}

		# Get regions
		$regions_text = array();
		if ($data["u_region"] != "") {
			$data["u_region"] = explode(",", $data["u_region"]);
			foreach ($data["u_region"] as $region_id) {
				$regions_text[] = $EXTERNAL_CONSTANTS["regions"][$region_id];
			}
			$regions_text = implode(", ", $regions_text);
		} else {
			$regions_text = "";
		}

		# Show details
		addContent($image);
		addContent("<h3>Details:</h3><p>Website: <a target=\"_blank\" href=\"" . fixURL($data["u_url"]) . "\">" . fixURL($data["u_url"]) . "</a></p>" .
			"<p>Region(s): " . $regions_text . "</p>" .
			"<p>Category(s): " . $categories_text . "</p>" .
			"<p>Description: " . $data["u_desc"] . "</p>");
		
		addContent("<h3>Contact:</h3>" .
			"<p>Business Email: <a href=\"mailto:" . $data["u_email"] . "\">Email this business</a></p>" .
			"<p>Business Phone: " . $data["u_phone"] . "</p>" . 
			"<p>Business Fax: " . $data["u_fax"] . "</p>" . 
			"<p>Business Mobile: " . $data["u_mobile"] . "</p>" .
			"<p>Address: " . $data["u_address"] . "</p>"); 


		# Get any people associated with this business
		$people = getRecord("SELECT u_id, u_fname, u_lname, u_email, u_phone, u_fax, u_mobile, u_address FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_business_name = '" . $passed_business_id . "'");

		# If any found
		if (count($people) > 0) {

			addContent("<h3>People (" . count($people) . "):</h3>");

			foreach ($people as $people_count => $person) {

				addContent("<h4>" . $person["u_fname"] . " " . $person["u_lname"] . "</h4>" .
					"<ul class=\"persondetails\">" .
						"<li>Email: <a href=\"mailto:" . $person["u_email"] . "\">" . $person["u_email"] . "</a></li>" .
						"<li>Phone: " . $person["u_phone"] . "</li>" .
						"<li>Fax: " . $person["u_fax"] . "</li>" .
						"<li>Mobile: " . $person["u_mobile"] . "</li>" .
						"<li>Address: " . $person["u_address"] . "</li>" .
					"</ul>");
			}

		} else {

			addContent("<h3>People:</h3>" .
				"<p>There are no contacts recorded for this business.</p>");
		}

	} else {

		addContent("<p>Sorry, an error occured and the business could not be found.</p>");
	}

	# Link back
	addContent("<p><strong><a href=\"?businesssearch=" . $business_search . "\">&laquo; Return to Business member listing</a></strong></p>");

} else {

	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# GET EXISTING CONTENT
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	$page_content = parseFlashLinks(preg_replace(array("/<br>/i", "/\n/i"), array("<br />", ""), parseFlashXML($PAGE -> getData("p_content"))));

	# Extract sidebar
	$second_column_content = extractSidebar($page_content);

	# Remove sidebar from content
	$page_content = removeSidebar($page_content);

	# Don't add just yet, but may in future
	addContent($page_content);


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# LIST OF BUSINESSES
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	# Current loop through letter pos
	$current_letter_pos = -1;

	# Array of letter groups
	$letter_groups = array_merge(array("0-9"), $VALID_CHARS_LETTERS_UPPER);

	# Create SQL
	$sql = "SELECT u_login_name, u_id FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_group = " . getBusinessGroupID();
	if ($business_search != "") {
		$sql .= " AND u_login_name LIKE '%" . $business_search . "%'";
	}
	$sql .= " ORDER BY u_login_name ASC";

	# Get data
	$businesses = getRecord($sql);

	# Assign a letter
	foreach ($businesses as $counter => $business) {
		
		# Get letter pos
		$letter_pos = array_search(strtoupper($business["u_login_name"][0]), $letter_groups);

		if ($letter_pos !== false) {
			$businesses[$counter]["letter"] = $letter_pos;
		} else {
			$businesses[$counter]["letter"] = 0;
		}
	}

	# Link to meeting minutes
	addContent("<h4>Meeting Minutes</h4><p>View the latest <a href=\"aboutmembersmm.php\">meeting minutes</a>.</p>");

	# Link to media
	addContent("<h4>Media</h4><p>View the <a href=\"members_media.php\">Members Media</a> page.</p>");

	# Description
	addContent("<h4>Browse Businesses</h4>");
	if ($business_search == "") {
		addContent("<p><a name=\"top\"></a>These are the business members of the site. Enter a search term to find a specific member, or browse the full list below.</p>");
	} else {
		addContent("<p><a name=\"top\"></a>These are the results of the search. Click 'View All' to see all businesses.</p>");
	}

	# Search
	addContent("<form name=\"bsearchform\" id=\"bsearchform\" action=\"" . $PAGE_ID . ".php\" method=\"get\">" .
		"<p>Search for: <input type=\"text\" name=\"businesssearch\" id=\"businesssearch\" size=\"30\" value=\"" . $business_search . "\" /> <input type=\"submit\" value=\"Search\" /> <input type=\"button\" onclick=\"document.getElementById('businesssearch').value='';document.getElementById('bsearchform').submit();\" value=\"View All\" /></p>" .
		"</form>");

	# List of links
	addContent("<p>Jump to letter: ");
	foreach ($letter_groups as $letter_count => $letter) {
		addContent("<a href=\"#letter" . $letter_count . "\">" . $letter . "</a> ");
	}
	addContent("</p>");

	# Open table
	addContent("<table class=\"businesslist\" cellspacing=\"0\">" .
		"<tr><th>Business</th><th>Details</th></tr>");

	# Print businesses
	foreach ($businesses as $counter => $business) {

		# Only print out the last new letter
		$new_letter_html = "";

		# See if we are printing a new letter
		while ($business["letter"] != $current_letter_pos) {
			$new_letter_html = "<tr class=\"letter\"><td colspan=\"2\"><a name=\"letter" . ($current_letter_pos + 1) . "\"></a><strong>" . $letter_groups[$current_letter_pos + 1] . "</strong> <sup><a href=\"#top\">[top]</a></sup></td></tr>";
			$current_letter_pos++;
		}

		addContent($new_letter_html);

		addContent("<tr><td>" . $business["u_login_name"] . "</td><td><a href=\"?business=" . $business["u_id"] . "&businesssearch=" . $business_search . "\">Details</a></td></tr>");
	}

	addContent("</table>");
}

?>